/*
 * Copyright 2007 Jeffrey Palm
 */

import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.*;

public class FeedList extends AbstractList {

  private final Feed feed;

  void addRest() {
   
  }

  void ok(Object value, Bloglines midlet) {
    final Item it = (Item)value;
    ItemForm itf = new ItemForm(midlet,FeedList.this,it);
    midlet.setCurrent(itf);
  }

  FeedList(final Bloglines midlet, final Displayable prev, final Feed feed) {
    super(midlet,feed.getTitle(),prev);
    this.feed = feed;
  }


  void parseRest(Vector subs) {
    boolean inItem = false;
    Item it = null;
    StringBuffer description = null;
    boolean inDescription = false;
    for (int i=0; i<subs.size(); i++) {
      String line = String.valueOf(subs.elementAt(i));
      if (line == null) continue;
      line = line.trim();
      if (line.startsWith("<item>")) {
	inItem = true;
	it = new Item();
      } else if (line.startsWith("</item>")) {
	inItem = false;
	String str = it.getTitle();
	append(it,str);
      } else if (inItem) {
	if (line.startsWith("<title>")) {
	  it.setTitle(Util.getBetweenTags(line));
	} else if (line.startsWith("<bloglines:itemid>")) {
	  String id = Util.getBetweenTags(line);
	  it.setProperty("id",id);
	} else if (line.startsWith("<pubDate>")) {
	  it.setDate(Util.getBetweenTags(line));
	} else if (line.startsWith("<link>")) {
	  it.setLink(Util.getBetweenTags(line));
	} else {
	  if (line.startsWith(DESCRIPTION_START)) {
	    String rest = line.substring(DESCRIPTION_START.length());
	    description = new StringBuffer();
	    description.append(rest);
	    inDescription = false;
	  } else if (line.endsWith(DESCRIPTION_END)) {
	    String rest = line.substring(0,line.indexOf(DESCRIPTION_END));
	    description.append(rest);
	    it.setDescription(Util.removeTags(description.toString()));
	    inDescription = false;
	  } else if (inDescription) {
	    description.append(line);
	  }
	}
      }
    }
  }

  private final static String DESCRIPTION_START = "<description><![CDATA[";
  private final static String DESCRIPTION_END   = "]]></description>";

}
